<template>
	<div class="addProject business-workAdd">
		<div class="addForm zll-form">
			<el-form :model="addForm" :rules="rules" ref="addForm" class="demo-ruleForm">
				<el-form-item class="formList" prop="num" label="条码数量：">
					<el-input
						clearable
						v-model.number="addForm.num"
						placeholder="请输入1-2000之间的数字"
						@input="handleInput"
					></el-input>
				</el-form-item>
			</el-form>
			<div class="tip">请输入1-2000任意数，系统会自动分配对应数量条码并自动打印</div>
		</div>
	</div>
</template>
<script>
import {Loading} from 'element-ui';
import { getStore } from "@/plugins/localStore";
import { formatDate } from "@/plugins/formatDate";
export default {
	props: ['editData'],
	data() {
		const validateNumber = (rule, value, callback) => {
			if (value === '' || value === null) {
				callback(new Error('请输入条码数量'));
			} else if (isNaN(value)) {
				callback(new Error('必须输入数字'));
			} else {
				callback();
			}
		};

		const validateRange = (rule, value, callback) => {
			if (value < 1) {
				callback(new Error('数量不能小于1'));
			} else if (value > 2000) {
				callback(new Error('数量不能大于2000'));
			} else {
				callback();
			}
		};

		return {
			addForm: {
				num: '',
			},

			count:0,
		}
	},
	methods: {
		// 处理输入，限制只能输入数字
		handleInput(value) {
			// 过滤非数字字符
			if (value && !/^\d*$/.test(value)) {
				this.addForm.num = value.replace(/[^\d]/g, '');
			}
			// 限制最大值
			if (value > 2000) {
				this.addForm.num = 2000;
			}
		},

		submitForm(val, type) {
			let self = this;

			this.$refs.addForm.validate((valid) => {
				if (!valid) {
					self.$message.warning('请输入有效的条码数量!');
					return;
				}

				if (!self.addForm.num || self.addForm.num < 1 || self.addForm.num > 2000) {
					self.$message.warning('请输入1-2000之间的数字!');
					return;
				}
				let printTime = formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss');
				let param = {num: self.addForm.num,printUserName: getStore().userName, printTime: printTime};
				let loadingInstance = Loading.service({
					lock: true,
					text: '正在发送打印指令...',
					spinner: 'el-icon-loading',
					background: 'rgba(0, 0, 0, 0.7)'
				});
				self.$http({
					url: "/jinl/box/batchBoxManage",
					method: "post",
					data: JSON.stringify(param),
					dataType: 'json',
					contentType: "application/json",
				}).then(resp => {
					loadingInstance.close();
					// if (resp.success) {
					// 	self.$message.success('打印指令发送成功！');
						
					// 	return;
					// }
					self.$eventBus.$emit("updateBoxList");
					self.$message.success(resp.message);
					self.count = 0;
				});
			});
		},
	},

	watch: {
		editData(val) {
			// 监听editData变化
		},
	},

	mounted() {
		// 组件挂载后的逻辑
	},
}
</script>
<style lang="scss" scoped>
@import "@/style/dialog.scss";

.formList {
	height: 32px;
}

.business-workAdd {
	.tip {
		text-align: center;
		font-size: 12px;
		position: relative;
		top: -10px;
		left: 10px;
		color: #f56c6c;
	}
}
</style>